/**
 * 文件名称:  MenuAction.java
 * 所属版权:  ChinaStar Technologies Co., Ltd. Copyright 2010-2021, All rights reserved
 * 简单描述:  <描述>
 * 修改作者:  xdli
 * 修改时间:  2012-6-20
 * 修改内容:  <修改内容>
 */
package com.chinastar.iweb.menu.action;

import java.io.PrintWriter;
import java.util.List;

import com.chinastar.iweb.common.struts.StarAction;
import com.chinastar.iweb.common.util.JsonUtils;
import com.chinastar.iweb.common.util.TreeConsult;
import com.chinastar.iweb.menu.domain.MenuBean;
import com.chinastar.iweb.menu.manager.MenuManager;

/**
 * MenuAction
 *  
 * @author  xdli
 * @version 2012-6-20
 * @see     MenuAction
 * @since   1.0
 */
public class MenuAction extends StarAction
{

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = 3829832742962244518L;
    
    private MenuManager menuManager = null;
    
    private MenuBean menuBean = new MenuBean();

    /**
     * 用户菜单树加载
     * @return String    JSON
     * @throws Exception Struts2 Exception
     */
    public String loadTree() throws Exception
    {
        PrintWriter out = getPrintWrite();
        String nodeid = getRequest().getParameter("node");
        if (TreeConsult.MENU_ROOT_ID_SELECT.equals(nodeid))
        {
            nodeid = "root";
        }
        List<MenuBean> menuList = menuManager.loadMenu(null, nodeid);
        String json = JsonUtils.ConverListToJson(menuList);
        out.print(json);
        if (null != out)
        {
            out.flush();
            out.close();
        }
        return null;
    }
    
    /**
     * 新增菜单节点跳转
     * @return 
     * @throws Exception
     */
    public String insert() throws Exception
    {
        return SUCCESS;
    }
    
    /**
     * 获取菜单节点信息
     */
    public String get() throws Exception
    {
        MenuBean bean = menuManager.get(menuBean.getNum());
        getRequest().setAttribute("menuBean", bean);
        return SUCCESS;
    }
    
    /**
     * 保存菜单节点
     */
    public String save() throws Exception
    {
        boolean isSave = menuManager.save(menuBean);
        List<MenuBean> menuList = menuManager.loadMenu(null, menuBean.getNum());
        getRequest().setAttribute("isSave", isSave);
        getRequest().setAttribute("menuList", menuList);
        return SUCCESS;
    }
    
    /**
     * 菜单节点数据更新
     */
    public String update() throws Exception
    {
        String message = null;
        boolean isUpdate = menuManager.update(menuBean);
        if (isUpdate)
        {
            message = "数据更新成功！";
        }
        else
        {
            message = "数据更新失败！";
        }
        getRequest().setAttribute("message", message);
        return SUCCESS;
    }
    
    public String delete() throws Exception
    {
        
        return SUCCESS;
    }
    
    public MenuBean getMenuBean()
    {
        return menuBean;
    }

    public void setMenuBean(MenuBean menuBean)
    {
        this.menuBean = menuBean;
    }

    public MenuManager getMenuManager()
    {
        return menuManager;
    }

    public void setMenuManager(MenuManager menuManager)
    {
        this.menuManager = menuManager;
    }
    
}
